/*
 * ButtonPanelHandler
 * 
 * Version: 1.0: This class is responsible for generating Button Panel which lets users navigate to previous or next
 * screen from where he currently is.
 * 
 * Date: 12/05/2010
 * 
 * Licensed under Eclipse open source license
 */

package slhs.tempo.scriptGenerator;

import java.awt.CardLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.mlc.swing.layout.LayoutConstraintsManager;

public class ButtonPanelHandler extends JPanel implements PanelHandler {

    private final int xPosition = 400;
    private final int yPosition = 200;
    private final int width = 500;
    private final int height = 250;
    public static final String SEGMENTS_CONFIGURATION_SCREEN_CARD = "Segment Configuration Screen";

    private final JFrame startupFrame;

    private final JPanel buttonPanel;
    private final JPanel screensHolderPanel;

    private JButton backButton;
    private JButton nextButton;

    private final CardLayout basePanelLayout;

    private SegmentConfigurationPanelHandler segmentConfigurationPanelHandler;
    private JPanel segmentConfigurationPanel;

    private static final String BUTTON_PANEL_LAYOUT_XML_FILE = "BUTTON_PANEL_LAYOUT.XML";

    public ButtonPanelHandler(JPanel screensHolderPanel, JFrame startupFrame) {

	this.screensHolderPanel = screensHolderPanel;
	startupFrame.setResizable(true);
	this.startupFrame = startupFrame;
	this.basePanelLayout = (CardLayout) (screensHolderPanel.getLayout());
	buttonPanel = new JPanel();
	GroupLayout buttonPanelLayout = new GroupLayout(buttonPanel);
	buttonPanel.setLayout(buttonPanelLayout);

	// initialize components to be added
	initComponents();
	addComponents(buttonPanelLayout);

    }

    private void initComponents() {
	backButton = new JButton("Back");
	backButton.addActionListener(this);
	nextButton = new JButton("Next");
	nextButton.addActionListener(this);

    }

    private void addComponents(GroupLayout buttonPanelLayout) {

	LayoutConstraintsManager layoutConstraintsManager = LayoutConstraintsManager.getLayoutConstraintsManager(this
		.getClass().getResourceAsStream(BUTTON_PANEL_LAYOUT_XML_FILE));
	LayoutManager panelLayout = layoutConstraintsManager.createLayout("panel", this);
	buttonPanel.setLayout(panelLayout);

	// here we add the controls to the container.
	buttonPanel.add(backButton, "backButton");
	buttonPanel.add(nextButton, "nextButton");

    }

    @Override
    public JPanel getPanel() {
	return buttonPanel;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
	Object source = event.getSource();

	if (source.equals(backButton)) {
	    basePanelLayout.show(screensHolderPanel, ScriptGeneratorUI.MODE_SELECTOR_SCREEN_CARD);
	    startupFrame.setBounds(xPosition, yPosition, width, height);
	} else if (source.equals(nextButton)) {
	    startupFrame.setTitle(startupFrame.getTitle() + "- Segment Configuration");
	    startupFrame.setSize(650, 1000);
	    if (segmentConfigurationPanelHandler == null) {
		segmentConfigurationPanelHandler = new SegmentConfigurationPanelHandler(screensHolderPanel);
		segmentConfigurationPanel = segmentConfigurationPanelHandler.getPanel();

		screensHolderPanel.add(segmentConfigurationPanel, SEGMENTS_CONFIGURATION_SCREEN_CARD);

	    }
	    basePanelLayout.show(screensHolderPanel, SEGMENTS_CONFIGURATION_SCREEN_CARD);
	}

    }
}
